﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;
using ESOL.Business.Mst;

namespace ESOL.Business.Bpt
{
    /// <summary>
    /// BptBlueprintChanges
    /// </summary>
    public partial class BptBlueprintChangesBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBptBlueprintChangesEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BptBlueprintChangesEntity GetBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity)
        {
            return DataAccess.SelectSingle<BptBlueprintChangesEntity>(objBptBlueprintChangesEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBptBlueprintChangesEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity)
        {
            return DataAccess.Insert(objBptBlueprintChangesEntity);
        }

        public static bool InsertBptBlueprintChanges(List<BptBlueprintChangesEntity> objBptBlueprintChangesEntityList)
        {
            return DataAccess.Insert(objBptBlueprintChangesEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBptBlueprintChangesEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity)
        {
            return DataAccess.Update(objBptBlueprintChangesEntity);
        }

        public static bool UpdateBptBlueprintChanges(List<BptBlueprintChangesEntity> objBptBlueprintChangesEntityList)
        {
            return DataAccess.Update<BptBlueprintChangesEntity>(objBptBlueprintChangesEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBptBlueprintChangesEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity)
        {
            return DataAccess.Delete(objBptBlueprintChangesEntity);
        }

        public static bool DeleteBptBlueprintChanges(List<BptBlueprintChangesEntity> objBptBlueprintChangesEntityList)
        {
            return DataAccess.Delete<BptBlueprintChangesEntity>(objBptBlueprintChangesEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBptBlueprintChangesAll()
        {
            return DataAccess.SelectAllDataSet<BptBlueprintChangesEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BptBlueprintChangesEntity> GetBptBlueprintChangesAllList()
        {
            return DataAccess.SelectAll<BptBlueprintChangesEntity>();
        }
        #endregion


        public static bool InsertBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity,List<BptBlueprintChangesItemEntity> items)
        {
            string code = MstCodeRuleBs.GetCurrentOrderCode("BptDrawPrintChanges");
            if (!string.IsNullOrEmpty(code))
            {
                objBptBlueprintChangesEntity.ChangesCode = code;
                foreach (BptBlueprintChangesItemEntity item in items)
                    item.ChangesCode = code;
            }
            else
                throw (new Exception("BptDrawPrintChanges 编码规则没有添加到数据库中！"));
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (objBptBlueprintChangesEntity != null) DataAccess.Insert(objBptBlueprintChangesEntity, broker);
                    if (items != null && items.Count > 0) DataAccess.Insert(items, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static bool UpdateBptBlueprintChanges(BptBlueprintChangesEntity objBptBlueprintChangesEntity,List<BptBlueprintChangesItemEntity> items)
        {
            List<BptBlueprintChangesItemEntity> deleteList = DataAccess.Select<BptBlueprintChangesItemEntity>(new BptBlueprintChangesItemEntity { ChangesCode = objBptBlueprintChangesEntity.ChangesCode });
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (objBptBlueprintChangesEntity != null) DataAccess.Update(objBptBlueprintChangesEntity, broker);
                    if (deleteList != null && deleteList.Count > 0) DataAccess.Delete(deleteList, broker);
                    if (items != null && items.Count > 0) DataAccess.Insert(items, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static DataTable GetBptBluePrintChangesByCondition(BptBlueprintChangesEntity condition)
        {
            string sql = @"Select   A.*,C.EMPLOYEE_NAME AS SPONSOR_PERSON_NAME,D.EMPLOYEE_NAME AS RECIVE_PERSON_NAME 
                        from dbo.BPT_BLUEPRINT_CHANGES A 
						left join dbo.HR_EMPLOYEE C on A.SPONSOR_PERSON=C.EMPLOYEE_NO  
                        left join dbo.HR_EMPLOYEE D on A.RECIVE_PERSON=D.EMPLOYEE_NO 
                        WHERE 1=1 ";
            if (condition != null)
            {

                if (!string.IsNullOrEmpty(condition.Id))
                    sql += " AND A.ID = '" + condition.Id + "'";
                    if (!string.IsNullOrEmpty(condition.ChangesCode))
                        sql += " AND A.CHANGES_CODE  LIKE '%" + condition.ChangesCode + "%'";

                    if (!string.IsNullOrEmpty(condition.Status))
                        sql += " AND A.STATUS = '" + condition.Status + "'";
                    if (!string.IsNullOrEmpty(condition.PlanOrderId))
                        sql += " AND A.PLAN_ORDER_ID  LIKE '%" + condition.PlanOrderId + "%'";
                    if (!string.IsNullOrEmpty(condition.ProdApplicationItemId))
                        sql += " AND A.PROD_APPLICATION_ITEM_ID LIKE '%" + condition.ProdApplicationItemId + "%'";
                    if (!string.IsNullOrEmpty(condition.SponsorPerson))
                        sql += " AND A.SPONSOR_PERSON  LIKE '%" + condition.SponsorPerson + "%'";
                if(condition.SponsorDate > DateTime.MinValue)
                {
                    DateTime st=new DateTime(condition.SponsorDate.Year,condition.SponsorDate.Month,condition.SponsorDate.Day);
                    DateTime et=new DateTime(condition.SponsorDate.Year,condition.SponsorDate.Month,condition.SponsorDate.Day,23,59,59);
                      sql += " AND A.SPONSOR_DATE > '" + st+ "' and A.SPONSOR_DATE <'"+et+"'";
                }
               
            }

            sql += " ORDER BY SPONSOR_DATE DESC ";

            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

        public static DataTable GetBptBluePrintChangesItemsByCondition(BptBlueprintChangesEntity condition)
        {
            string sql = @"Select   *
                        from dbo.BPT_BLUEPRINT_CHANGES_ITEM A 
						left join dbo.BPT_BLUEPRINT_CHANGES b on b.ID=a.CHANGES_ID  
                        LEFT JOIN dbo.MST_PRODUCT C ON A.PRODUCT_ID=C.PRODUCT_ID
                        WHERE 1=1 ";
            if (condition != null)
            {

                if (!string.IsNullOrEmpty(condition.Id))
                    sql += " AND b.ID = '" + condition.Id + "'";
                if (!string.IsNullOrEmpty(condition.ChangesCode))
                    sql += " AND b.CHANGES_CODE = '" + condition.ChangesCode + "'";
                if (!string.IsNullOrEmpty(condition.Status))
                    sql += " AND b.STATUS = '" + condition.Status + "'";

            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

    }
}
